package com.pipayshop.controller;

import com.pipayshop.domain.dto.ApplyShopCommodityDTO;
import com.pipayshop.domain.dto.StoreCommodityInfoDTO;
import com.pipayshop.domain.vo.*;
import com.pipayshop.mapper.ImageMapper;
import com.pipayshop.service.PhysicalStoreCommodityInfoService;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 实体店的商品表 前端控制器
 * </p>
 *
 * @author zxb
 * @since 2023-07-27
 */
@Api(value = "实体店的商品表", tags = "实体店的商品表")
@RestController
@RequestMapping("/pipayshopapi/store-commodity-info")
public class PhysicalStoreCommodityInfoController {

    @Resource
    private PhysicalStoreCommodityInfoService shopCommodityService;

    @Resource
    private ImageMapper imageMapper;

    @GetMapping("getResidueByCommodityId/{CommodityId}")
    @ApiOperation("根据商品id获取商品库存")
    public ResponseVO getResidueByCommodityId(@PathVariable("CommodityId")String commodityId){
        try {
            Integer Residue = shopCommodityService.getResidueByCommodityId(commodityId);
            if (Residue == null) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(Residue);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("query_failed_stock_failed");
        }
    }

    @PostMapping("issueShopCommodity")
    @ApiOperation("发布实体店商品")
    public ResponseVO<String> issueShopCommodity(@RequestBody ApplyShopCommodityDTO applyShopCommodityDTO) {
        try {
            shopCommodityService.issueShopCommodity(applyShopCommodityDTO);
            return ResponseVO.getSuccessResponseVo("store_product_publish_success");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("failed");
        }
    }

    @GetMapping("selectShopInfoListByShopId/{shopId}")
    @ApiOperation("根据店铺id查找实体店商品的列表(商品帶标签)")
    public ResponseVO<List<StoreCommodityInfo1VO>> selectShopInfoListByShopId(@PathVariable String shopId) {
        try {
            List<StoreCommodityInfo1VO> pageDataVO = shopCommodityService.selectShopInfoListByShopId(shopId);
            return ResponseVO.getSuccessResponseVo(pageDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("get_product_detail_info_list_failed");
        }
    }

    @GetMapping("selectShopInfoByCommodityId/{commodityId}")
    @ApiOperation("根据商品的id查找实体店商品的详情信息")
    public ResponseVO<StoreDetailInfoVO> selectShopInfoByCommodityId(@PathVariable String commodityId) {
        try {
            StoreDetailInfoVO shopDetailInfoVO = shopCommodityService.selectShopInfoByCommodityId(commodityId);
            return ResponseVO.getSuccessResponseVo(shopDetailInfoVO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("get_product_detail_info_list_failed");
        }
    }

    @GetMapping("selectCommodityByShopIdAndStatus/{shopId}")
    @ApiOperation("根据店铺id查询商品列表(审核相关)")
    public ResponseVO<List<ApplicationRecordVO>> selectCommodityByUidAndStatus(@PathVariable("shopId")String shopId) {
        try {
            List<ApplicationRecordVO> list = shopCommodityService.selectCommodityByUidAndStatus(shopId);
            return ResponseVO.getSuccessResponseVo(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("commodity_status_retrieval_failed");
        }
    }

    @GetMapping("selectStatusListByShopId")
    @ApiOperation("根据店铺id查找实体店商品的上架和下架列表")
    public ResponseVO<PageDataVO> selectStatusListByShopId(CommodityStatusPageVO commodityStatusPageVO) {
        try {
            PageDataVO pageDataVO = shopCommodityService.selectStatusListByShopId(commodityStatusPageVO);
            return ResponseVO.getSuccessResponseVo(pageDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("find_store_commodity_list_failed");
        }
    }

    @PostMapping("updateCommodityStatus/{commodityId}/{status}")
    @ApiOperation("根据商品id，更改商品的上下架状态")
    public ResponseVO<String> updateCommodityStatus(@PathVariable String commodityId, @PathVariable Integer status) {
        try {
            boolean result = shopCommodityService.updateCommodityStatus(commodityId, status);
            if (!result) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("根据商品id，更改商品的上下架状态成功");
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e) {
            throw new BusinessException("update_commodity_shelves_status_failed");
        }
    }

    @PostMapping("updateCommodityUp/{commodityId}")
    @ApiOperation("根据商品id，上架变为下架")
    public ResponseVO<String> updateCommodityUp(@PathVariable String commodityId) {
        try {
            boolean result = shopCommodityService.updateCommodityUp(commodityId);
            if (!result) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("根据商品id，上架变为下架成功");
        } catch (Exception e) {
            throw new BusinessException("modify_shelves_status_failed");
        }
    }

    @PostMapping("updateCommodityCheck/{commodityId}")
    @ApiOperation("根据商品id，下架")
    public ResponseVO<String> updateCommodityCheck(@PathVariable String commodityId) {
        try {
            boolean result = shopCommodityService.updateCommodityCheck(commodityId);
            if (!result) {
                throw new BusinessException("service_exception_contact_admin");
            }
            return ResponseVO.getSuccessResponseVo("根据商品id，下架变为审核中成功");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("offline_failed");
        }
    }

    @PostMapping("shopCommodityTopImageUp")
    @ApiOperation("实体店商品展示图上传")
    public ResponseVO<String> shopCommodityTopImageUp(MultipartFile multipartFile){
        try {
            String imageId = shopCommodityService.shopCommodityTopImageUp(multipartFile);
            if ("image_not_approved".equals(imageId)){
                return ResponseVO.getFalseResponseMsg("image_not_approved");
            }
            return ResponseVO.getSuccessResponseVo(imageId);
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("commodity_display_image_upload_failed");
        }
    }

    @GetMapping("/GetImage/{imgId}")
    @ApiOperation("实体店商品展示图上传")
    public ResponseVO<String> getImage(@PathVariable String imgId){
        try {
            return ResponseVO.getSuccessResponseVo(imageMapper.selectPath(imgId));
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("store_commodity_display_image_upload_failed");
        }
    }

    @PostMapping("shopCommodityImageUp")
    @ApiOperation("实体店商品轮播图上传")
    public ResponseVO shopCommodityImageUp(@RequestBody MultipartFile[] file){
        try {
                List<String> imageIdList = shopCommodityService.shopCommodityImageUp(file);
            for (int i = 0; i < imageIdList.size(); i++) {
                if ("image_not_approved".equals(imageIdList.get(i))){
                    return ResponseVO.getFalseResponseMsg("image_not_approved");
                }
            }
            return ResponseVO.getSuccessResponseVo(imageIdList);
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("store_commodity_carousel_image_upload_failed");
        }
    }

//    @PostMapping("hotelRoomCommodityImageUp")
//    @ApiOperation("实体店商品轮播图上传，返回url和id")
//    public ResponseVO hotelRoomCommodityImageUp(@RequestBody MultipartFile[] file){
//        try {
//            Map<String,String> imageIdList = shopCommodityService.hotelRoomCommodityImageUp(file);
//            for (int i = 0; i < imageIdList.size(); i++) {
//                if ("image_not_approved".equals(imageIdList.get(i))){
//                    return ResponseVO.getFalseResponseMsg("image_not_approved");
//                }
//            }
//            return ResponseVO.getSuccessResponseVo(imageIdList);
//        }catch (Exception e){
//            e.printStackTrace();
//            throw new BusinessException("store_commodity_carousel_image_upload_failed，请联系后台人员");
//        }
//    }

    @PostMapping("shopCommodityImageUpUrl")
    @ApiOperation("实体店商品轮播图上传")
    public ResponseVO shopCommodityImageUpUrl(@RequestBody MultipartFile[] file){
        try {
            List<String> imageIdList = shopCommodityService.shopCommodityImageUp(file);
            for (int i = 0; i < imageIdList.size(); i++) {
                if ("image_not_approved".equals(imageIdList.get(i))){
                    return ResponseVO.getFalseResponseMsg("image_not_approved");
                }
            }
            // 调用imageMapper.selectPath查询所有图片url
            List<String> imageUrlList = new ArrayList<>();
            for (String imageId : imageIdList) {
                imageUrlList.add(imageMapper.selectPath(imageId));
            }
            return ResponseVO.getSuccessResponseVo(imageUrlList);
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("commodity_carousel_image_upload_failed");
        }
    }

    @PostMapping("updateCommodityStatus")
    @ApiOperation("根据商品id更改商品的信息")
    public ResponseVO<String> updateCommodityStatus(@RequestBody StoreCommodityInfoDTO shopCommodityInfoDTO) {
        try {
            // 调用服务层更新商品状态的方法
            boolean result = shopCommodityService.updateCommodity(shopCommodityInfoDTO);

            if (result) {
                return ResponseVO.getSuccessResponseVo("根据商品id，更改商品的信息成功");
            } else {
                throw new BusinessException("update_commodity_by_id_failed");
            }
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e) {
            // 捕获其他未知异常并返回通用错误信息
            throw new BusinessException("failed");
        }

    }

    @DeleteMapping("delete/{commodityId}")
    @ApiOperation("根据商品id删除商品的信息")
    public ResponseVO<String> deleteCommodity(@PathVariable String commodityId) {
        try {
            if (shopCommodityService.deleteCommodity(commodityId)) {
                return ResponseVO.getSuccessResponseVo("根据商品id，删除商品的信息成功");
            } else {
                throw new BusinessException("delete_commodity_by_id_failed");
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("delete_commodity_info_failed");
        }
    }



}


